
import { isNull } from "../Define";
import { IValue } from "../data/IValue";
import { IList } from "../data/IList";
export class LocalList<T = any> implements IList {

    protected value: IValue<T[]>

    constructor(value: IValue<T[]>) {
        this.value = value
    }

    setValue(v: T[], isSave: boolean = true) {
        this.value.setValue(v, isSave)
    }

    getValue(): T[] {
        return this.value.getValue();
    }

    has(v: T): boolean {
        let list = this.getValue();
        let index = list.indexOf(v)
        return index >= 0
    }

    get(index: number): T {
        let list = this.getValue();
        if (index >= 0 && index <= list.length - 1) {
            return list[index];
        }
        return null;
    }

    clear() {
        this.value.clear();
    }

    size(): number {
        let list = this.getValue();
        return list.length;
    }

    push(value: T, save: boolean = true) {
        let list = this.value.getValue();
        list.push(value)
        if (save) {

        }
    }

    save() {
        this.value.save();
    }

    set(index: number, data: T, save: boolean = true) {
        let list = this.getValue();
        list[index] = data;
        if (save) {
            this.save();
        }
    }

    remove(index: number): void {
        let list = this.getValue();
        if (!isNull(list[index])) {
            list.splice(index, 1)
            // delete this.value[index]
            this.save();
        }
    }

    hasData() {
        return this.value.hasData();
    }
}